package com.cyt.chattlibrary.face;/**
 * Created by cpoopc on 2015/9/1.
 */

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.cyt.chattlibrary.R;

import java.util.List;
import java.util.Map;

/**
 * 内含SmileyView,放入viewpager中的layout
 * User: cpoopc
 * Date: 2015-09-01
 * Time: 18:30
 * Ver.: 0.1
 */
public class EmotionGrid extends GridView {

    private SimpleAdapter smileyAdapter;

    public EmotionGrid(Context context) {
        super(context);
        init();
    }

    public EmotionGrid(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EmotionGrid(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public EmotionGrid(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setNumColumns(7);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredHeight = getMeasuredHeight();
        int height = MeasureSpec.getSize(heightMeasureSpec) * 4 / 5;
        if (height != 0 && height == measuredHeight) {
            return;
        }
        setMeasuredDimension(getMeasuredWidth(), height);
    }



    @Override
    protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
        super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
    }

    public void bindData(List<Map<String, Object>> emotionEntities) {
        if (smileyAdapter == null) {
            smileyAdapter = new SimpleAdapter(getContext(), emotionEntities, R.layout.team_layout_single_expression_cell, new String[]{"image"}, new int[]{R.id.image});
        }
        setAdapter(smileyAdapter);
    }

}
